<?php
namespace app\index\controller;

use app\helper\Helper;
use think\Cache;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Index extends Base
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * 首页
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        //10CA1 5F81F C70BC B00C9 07C73 D
        //2BCA2 CEE28 D000D 007CE 3C8C4 2
        //29F31 4C411 C609C 80FC6 04C43 A
        //2BCA1 2CE08 F121F 7DC35 9B736 9

//        $url_page = "http://leagueskin.net/k.php";
//        $user_agent = "Mozilla/4.0";
//        $proxy = "http://125.210.188.36:80";    //此处为代理服务器IP和PORT
//
//        $res = $this->curl_string($url_page,$user_agent,$param,$this->setIp(),$this->setIp());
//        halt($res);
//
//        $output = substr($res,-25);
        $output = '';
        $code = json_decode(Cache::get('code'));

        if(!empty($code) && $code->time + 3600 > time()){
            $output = $code->code;
        }else{
            while (empty($output)){
                $output = $this->curl();
                $code = [
                    'code'=>$output,
                    'time'=>time(),
                ];
                Cache::set('code',json_encode($code));
            }
        }
        $this->assign('code',$output);
        return $this->fetch('index/code');
    }


    public function curl()
    {
        $param = [
            'KeyID'=>'1cc644c655af11d814e07fc8201bcd47',
            'v'=>time().rand(100,999)
        ];
        $url = 'http://leagueskin.net/k.php';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }


    public function curl_string ($url,$user_agent,$param,$ip1,$ip2){
        $ch = curl_init();
//        curl_setopt ($ch, CURLOPT_PROXY, $proxy);
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, "d:\cookies.txt");
        curl_setopt ($ch, CURLOPT_HEADER, 1);
        curl_setopt ($ch, CURLOPT_HTTPHEADER, array('CLIENT-IP:125.210.188.36', 'X-FORWARDED-FOR:125.210.188.36'));  //此处可以改为任意假IP
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
        $result = curl_exec ($ch);
        curl_close($ch);
        return $result;
    }

    public function setIp()
    {
        $a = rand(50,129);
        $b = rand(50,255);
        $c = rand(50,255);
        $d = rand(50,255);
        return $a.'.'.$b.'.'.$c.'.'.$d;
    }



    public function GetIP(){
        if($_SERVER['HTTP_CLIENT_IP']){
            $onlineip=$_SERVER['HTTP_CLIENT_IP'];
        }elseif($_SERVER['HTTP_X_FORWARDED_FOR']){
            $onlineip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $onlineip=$_SERVER['REMOTE_ADDR'];
        }
        return $onlineip;
    }


    public function bookClass()
    {
        $param = $this->request->param();
        if(!$this->uid){
           return Helper::error([],'您还没有登陆');
        }
        $data = [
            'book_id'=>$param['id'],
            'user_id'=>$this->uid,
        ];
        $res = Db::table('user_book')->where($data)->find();
        if($res){
            return Helper::error([],'您已经预定该课程了');
        }
        $data['created_ts'] = time();
        $data['status'] = 1;
        $res = Db::table('user_book')->insert($data);
        if($res){
            return Helper::success([],'预定成功');
        }else{
            return Helper::error([],'预定失败');
        }
    }

    public function contact()
    {
        $comment1 = Db::table('user_comment')->select();
        foreach ($comment1 as $k => $v){
            $user = Db::table('users')->where('id',$v['user_id'])->find();
            if(!empty($user)){
                $comment1[$k]['user_name'] = $user['user_name'];
            }else{
                $comment1[$k]['user_name'] = '游客';
            }
        }
        $this->assign('comment1',$comment1);
        $this->assign('count_comment',count($comment1));
        return $this->fetch('index/page');
    }

    public function addContact()
    {
        $param = $this->request->param();
        if(empty($param['comment'])){
            return Helper::error([],'请输入评论');
        }
        $uid = 0;
        if($this->uid){
            $uid = $this->uid;
        }
        $data = [
            'comment'=>$param['comment'],
            'user_id'=>$uid,
            'created_ts'=> time(),
        ];
        $res = Db::table('user_comment')->insert($data);
        if($res){
            return Helper::success([],'评论成功');
        }else{
            return Helper::error([],'评论失败');
        }
    }


    public function about()
    {
        return $this->fetch('index/about');
    }

    public function user()
    {
        $comment2 = Db::table('user_book')
            ->join('book','book.id=user_book.book_id')
            ->where('user_book.user_id',$this->uid)
            ->where('book.delete_ts',0)
            ->field('book.name,user_book.created_ts')
            ->select();
        $this->assign('comment2',$comment2);
        return $this->fetch('index/services');
    }
}
